package org.yzh.web.controller;

import com.google.gson.JsonObject;
import com.qiniu.processing.OperationStatus;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.yzh.web.component.mybatis.Page;
import org.yzh.web.component.mybatis.PageInfo;
import org.yzh.web.component.mybatis.Pagination;
import org.yzh.web.model.APIException;
import org.yzh.web.model.APIResult;
import org.yzh.web.model.enums.DefaultCodes;
import org.yzh.web.model.vo.*;
import org.yzh.web.service.VideoDownloadService;

/**
 * @author lsj
 * @date 2021/1/17 3:44
 */
@RestController
@Slf4j
public class VideoController {

    @Autowired
    private VideoDownloadService videoDownloadService;

    @Operation(summary = "发送下载视频请求")
    @PostMapping("videos")
    public APIResult<Object> addVideoDownload(@RequestBody VideoDownloadForm form) {
        try {
            int status = videoDownloadService.addVideoDownload(form);
            return APIResult.SUCCESS;
        } catch (APIException e) {
            return APIResult.error(e);
        }
    }

    @Operation(summary = "视频下载历史")
    @GetMapping("videos")
    public APIResult<Pagination<VideoDownload>> findHistory(VideoDownloadQuery query, PageInfo pageInfo) {
        return APIResult
            .success((Pagination<VideoDownload>)Page.start(() -> videoDownloadService.findHistory(query), pageInfo));
    }

    @Operation(summary = "视频处理成功通知")
    @PostMapping("videos/persistent_notify")
    public APIResult<Object> persistentNotify(@RequestBody JsonObject status) {
        log.info("persistentNotify {}", status);
        return APIResult.success();
    }

}
